package com.boss.salary.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IConfigCtrlDao;
import com.boss.salary.entity.ConfigCtrl;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class ConfigCtrlDaoImpl  extends BaseDaoImpl implements IConfigCtrlDao {
    @Override
    public void saveConfigCtrl(ConfigCtrl cc) {
        StringBuilder sql = new StringBuilder();
        sql.append(" insert ").append(TableName.SAL_CONFIG_CTRL);
        sql.append(" (id, update_time, create_time, config_type, mof_div_code, is_able, config_type_name, is_deleted,ctrl_num) ");
        sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        Object[] objects = new Object[] {
                SysUtils.generateGuid(),String.valueOf(DateUtils.getCurrentTimeStamp()),String.valueOf(DateUtils.getCurrentTimeStamp()), cc.getConfigType(),
                cc.getMofDivCode(), cc.getIsAble(), cc.getConfigTypeName(),cc.getIsDeleted(),cc.getCtrlNum()
        };
        this.getjdbcTemplate().update(sql.toString(), objects);
    }

    @Override
    public void updateConfigCtrl(ConfigCtrl cc) {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE ").append(TableName.SAL_CONFIG_CTRL);
        sql.append(" SET update_time = ?, config_type = ?, mof_div_code = ?, is_able = ?, config_type_name = ?, is_deleted = ?,ctrl_num=? ");
        sql.append(" WHERE id = ? ");
        Object[] objects = new Object[] {
                String.valueOf(DateUtils.getCurrentTimeStamp()), cc.getConfigType(),
                cc.getMofDivCode(), cc.getIsAble(), cc.getConfigTypeName(),cc.getIsDeleted(),cc.getCtrlNum(),cc.getId()
        };
        this.getjdbcTemplate().update(sql.toString(), objects);
    }

    @Override
    public List<ConfigCtrl> queryConfigCtrlPage(PaginationDTO paginationDTO, Map<String, Object> queryParam) {
        return null;
    }

    @Override
    public int queryConfigCtrlTotil(Map<String, Object> queryParam) {
        return 0;
    }

    @Override
    public ConfigCtrl selectConfigCtrlByTypeMof(String type, String mof_div_code) {
        StringBuilder sql = new StringBuilder();
        sql.append("select id, update_time, create_time, config_type, mof_div_code, is_able, config_type_name, is_deleted,ctrl_num ")
                .append(" from ").append(TableName.SAL_CONFIG_CTRL);
               // .append(" where config_type=? and is_deleted=? and mof_div_code=? ");
//        Object[] params = new Object[]{
//                type,SystemDictConstant.NOT_DELETED, mof_div_code
//        };
        List<ConfigCtrl> cclist=this.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper(ConfigCtrl.class));//,params
        if(CollectionUtils.isEmpty(cclist)){
            return null;
        }
        return cclist.get(0);

    }

    @Override
    public List<ConfigCtrl> selectConfigCtrl(String province) {
        StringBuilder sql = new StringBuilder();
        sql.append("select  id, update_time, create_time, config_type, mof_div_code, is_able, config_type_name, is_deleted,ctrl_num ")
                .append("  from ")
                .append(TableName.SAL_CONFIG_CTRL);
        sql.append(" where  mof_div_code=? ");
        return this.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<>(ConfigCtrl.class), province);

    }



    public void updataIsAbleConfigCtrl(ConfigCtrl cc) {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE ").append(TableName.SAL_CONFIG_CTRL);
        sql.append(" SET update_time = ?, is_able = ?");
        sql.append(" WHERE id = ? ");
        Object[] objects = new Object[] {
                cc.getUpdateTime(),
                cc.getIsAble() , cc.getId()
        };
        this.getjdbcTemplate().update(sql.toString(), objects);

    }

    @Override
    public void deletedConfigCtrl(String province) {
        StringBuilder sql = new StringBuilder();
        sql.append(" DELETE  FROM ").append(TableName.SAL_CONFIG_CTRL);
        sql.append(" WHERE mof_div_code = ? and is_deleted = ?");
        this.getjdbcTemplate().update(sql.toString(), province,SystemDictConstant.NOT_DELETED);
    }

    @Override
    public void saveConfigCtrl(List<ConfigCtrl> configCtrls, String province) {
        StringBuilder sql = new StringBuilder();
        sql.append(" insert into ").append(TableName.SAL_CONFIG_CTRL);
        sql.append(" (id, update_time, create_time, config_type, mof_div_code, is_able, config_type_name, is_deleted,ctrl_num) ");
        sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?,?) ");
        List<Object[]> paramsList = new ArrayList<>(configCtrls.size());
        for (ConfigCtrl configCtrl : configCtrls) {
            Object[] params = new Object[]{
                    SysUtils.generateGuid(),String.valueOf(DateUtils.getCurrentTimeStamp()),String.valueOf(DateUtils.getCurrentTimeStamp()), configCtrl.getConfigType(),
                    province, configCtrl.getIsAble(), configCtrl.getConfigTypeName(), SystemDictConstant.NOT_DELETED, configCtrl.getCtrlNum()
            };
            paramsList.add(params);
        }
        super.getJdbcTemplate().batchUpdate(sql.toString(), new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return paramsList.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = paramsList.get(i);
                for(int a = 0; a < sme.length; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });
    }

//    public int selectBatchControl(ConfigCtrl configCtrl) {
//        StringBuilder sql = new StringBuilder();
//        sql.append(" SELECT count(1) FROM ").append(TableName.SAL_CONFIG_CTRL);
//        sql.append(" WHERE is_able = ? AND mof_div_code=? AND config_type = ? ");
//        Object[] objects = new Object[] {
////                configCtrl.getIsAble(),
////                configCtrl.getMofDivCode(),
////                configCtrl.getConfigType()
//        };
//        return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class,objects);//
//    }



}
